package com.uzhie.jt808.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * Created by TangFei on 2018/8/28.
 */
public class Client {

    private String host;
    private int port;
    private static Channel channel;

    public Client(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void start() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group) // 注册线程池
                    .channel(NioSocketChannel.class) // 使用NioSocketChannel来作为连接用的channel类
                    .handler(new ClientFilter());
            ChannelFuture sync = b.connect(this.host, this.port).sync();
//            channel = sync.channel();
            sync.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) {
        Client client = new Client("192.168.31.128", 18081);
        try {
            client.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
